package com.lsh.payment.core.service.payment.builder;

import com.alibaba.fastjson.JSON;
import com.lsh.payment.api.model.basevo.QueryContent;
import com.lsh.payment.core.constant.BusinessConstant;
import com.lsh.payment.core.model.payenum.PayStatus;
import com.lsh.payment.core.model.payenum.cmpay.CmBizOrderStatus;
import com.lsh.payment.core.model.payment.PayDeal;
import com.lsh.payment.core.strategy.vo.base.BasePayResponse;
import com.lsh.payment.core.strategy.vo.cmpay.CmQueryResponse;
import com.lsh.payment.core.util.BigDecimalUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * Project Name: lsh-payment
 *
 * @author peter
 * @date 16/11/27
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
public class CmQueryBuilder implements QueryBuilder {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private QueryContent content = new QueryContent();

    private PayDeal updatePaydeal = new PayDeal();

    @Override
    public void parse(BasePayResponse basePayResponse, PayDeal payDeal) {

        content.setTradeId(payDeal.getTradeId());
        content.setChannelTransaction(payDeal.getChannelTransaction());

        logger.info("[cm base query response is] : [" + JSON.toJSONString(basePayResponse) + "]");

        if (basePayResponse == null || basePayResponse.getCode() == null || basePayResponse.getCode().equals(BusinessConstant.PAY_REQUEST_FAIL)) {
            content.setPayCode(PayStatus.PAYING.getValue());
            content.setPayMsg(PayStatus.PAYING.getName());

        } else {
            CmQueryResponse cmQueryResponse = null;
            if (basePayResponse instanceof CmQueryResponse) {
                cmQueryResponse = (CmQueryResponse) basePayResponse;
            }

            if (cmQueryResponse == null) {
                content.setPayCode(PayStatus.PAYING.getValue());
                content.setPayMsg(PayStatus.PAYING.getName());
                return;
            }

            logger.info("[收钱吧 query response is] : [" + JSON.toJSONString(cmQueryResponse) + "]");

            content.setTradeId(payDeal.getTradeId());
            content.setChannelTransaction(cmQueryResponse.getSn());

            //参数校验
            if (this.cmCheckParam(cmQueryResponse, payDeal)) {

                String tradeOrderState = cmQueryResponse.getOrder_status();

                if (tradeOrderState.equals(CmBizOrderStatus.PAID.getCode())) {
                    //交易成功
                    content.setPayCode(PayStatus.PAY_SUCCESS.getValue());
                    content.setPayMsg(PayStatus.PAY_SUCCESS.getName());

                } else {
                    // TODO
                    content.setPayCode(PayStatus.PAYING.getValue());
                    content.setPayMsg(PayStatus.PAYING.getName());
                }
            }
        }
    }

    @Override
    public void createUpdatePaydeal(BasePayResponse basePayResponse, PayDeal payDeal) {

        updatePaydeal.setPayId(payDeal.getPayId());
        updatePaydeal.setQueryTime(new Date());
        updatePaydeal.setDoneTime(new Date());

        if (content == null || content.getPayCode() == null) {
            return;
        }

        CmQueryResponse cmQueryResponse = null;
        if (basePayResponse instanceof CmQueryResponse) {
            cmQueryResponse = (CmQueryResponse) basePayResponse;
        }
        if (cmQueryResponse == null) {
            return;
        }

        if (content.getPayCode().equals(PayStatus.PAY_SUCCESS.getValue())) {

            updatePaydeal.setPayStatus(PayStatus.PAY_SUCCESS.getValue());
            updatePaydeal.setChannelTransaction(cmQueryResponse.getSn());
            updatePaydeal.setReceiveAmount(BigDecimalUtils.fenToYuan(cmQueryResponse.getTotal_amount()));

            if(StringUtils.isBlank(cmQueryResponse.getFinish_time())){
                updatePaydeal.setPayTime(new Date());
            }else{
                updatePaydeal.setPayTime(new Date(Long.valueOf(cmQueryResponse.getFinish_time())));
            }
        } else if (content.getPayCode().equals(PayStatus.PAY_FAIL.getValue())) {

            updatePaydeal.setPayStatus(PayStatus.PAYING.getValue());
        }
    }

    /**
     * 拿到解析后的结果
     *
     * @return
     */
    public QueryContent getResult() {

        logger.info("收钱吧 parse content is [" + JSON.toJSONString(content) + "]");
        return content;
    }

    /**
     * 拿到解析后的结果
     *
     * @return
     */
    public PayDeal getUpdatePaydeal() {

        return updatePaydeal;
    }

    /**
     * 校验参数
     *
     * @param cmQueryResponse 返回数据
     * @param payDeal         支付记录
     * @return boolean
     */
    private boolean cmCheckParam(CmQueryResponse cmQueryResponse, PayDeal payDeal) {

        return true;
    }
}
